package cn.wolfcode.luowowo.comment.service.impl;

import cn.wolfcode.luowowo.article.domain.AnswerComment;
import cn.wolfcode.luowowo.cache.service.IUserRedisService;
import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.comment.query.AnswerCommentQuery;
import cn.wolfcode.luowowo.comment.repository.AnswerCommentRepository;
import cn.wolfcode.luowowo.comment.service.IAnswerCommentService;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcoe.luowowo.common.exception.LogicException;
import cn.wolfcoe.luowowo.common.util.AjaxResult;
import cn.wolfcoe.luowowo.common.util.DateUtil;
import cn.wolfcoe.luowowo.common.util.baiducheck.SensitiveWordsUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class AnswerCommentServiceImpl implements IAnswerCommentService {

    @Autowired
    private MongoTemplate template;

    @Autowired
    private AnswerCommentRepository repository;
    @Reference
    private IUserRedisService userRedisService;


    @Override
    public Page <AnswerComment> query(AnswerCommentQuery qo) {
        Query query = new Query();
        query.addCriteria( Criteria.where( "answerId" ).is( qo.getAnswerId() ) );
        //分页
        Pageable pageable = PageRequest.of(0,
                qo.getPageSize(), Sort.Direction.ASC, "releaseTime");
        query.with(pageable);

        //查count
        long total = template.count( query, AnswerComment.class );
        if (total == 0) {
            return Page.empty();
        }
        //查询所有记录数
        List <AnswerComment> list = template.find( query, AnswerComment.class );
        return new PageImpl <>( list, pageable, total );
    }

    @Override
    public AjaxResult saveOrUpdate(AnswerComment answer, UserInfo userInfo) {
        try {
            repository.save( answer );
            String intervalKey = RedisKeys.USER_COMMENT_INTERVAL.join( userInfo.getId().toString() );
            long between = DateUtil.getDateBetween( new Date(), DateUtil.getEndDate( new Date() ) ) / 60;
            //时间间隔
            userRedisService.setReids( intervalKey, "1", Integer.parseInt( between + "" ) );
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.SUCCESS;
    }

    @Override
    public void check(AnswerComment answer, UserInfo userInfo) throws LogicException {
        String interva = RedisKeys.USER_COMMENT_INTERVAL.join( userInfo.getId().toString() );
        String intervalCount = userRedisService.getUserInfoRedis( interva );

        if (intervalCount != null) {
            throw new LogicException( "请3分钟后再评论哦!" );
        }
        boolean spam = SensitiveWordsUtil.SensitiveWordCheck( answer.getContent() ).isSpam();
        //有垃圾信息
        if (spam) {
            String stateKey = RedisKeys.USER_COMMENT_STATE_DISABLE.join( userInfo.getId().toString() );
            String lineCountKey = RedisKeys.USER_COMMENT_LINE_COUNT.join( userInfo.getId().toString() );

            String state = userRedisService.getUserInfoRedis( stateKey );
            String lineCountStr = userRedisService.getUserInfoRedis( lineCountKey );
            int lineCount = 0;
            if (lineCountStr != null) {
                lineCount = Integer.parseInt( lineCountStr );
            }

            if (state != null) {
                throw new LogicException( "评论功能禁用!" );
            } else if (lineCountKey == null || lineCount < 3) {
                //添加违规次数
                long between = DateUtil.getDateBetween( new Date(), DateUtil.getEndDate( new Date() ) ) / 60;
                userRedisService.increaseLineCount( lineCountKey, 1, Integer.parseInt( between + "" ) );
                return;
            } else {
                //增加违规次数,同时添加违规状态
                long between = DateUtil.getDateBetween( new Date(), DateUtil.getEndDate( new Date() ) ) / 60;
                userRedisService.increaseLineCount( lineCountKey, 1, Integer.parseInt( between + "" ) );
                return;
            }


        }


    }

}
